package com.xcx.robot.mirai.factory.facade;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.xcx.robot.mirai.entity.JX3API;
import com.xcx.robot.mirai.factory.BaseFacade;
import com.xcx.robot.mirai.jxapi.Result;
import com.xcx.robot.mirai.jxapi.ResultData;
import com.xcx.robot.mirai.key.KeyWord;
import com.xcx.robot.mirai.util.SenderHelper;
import com.xcx.robot.util.DateUtils;
import com.xcx.robot.util.LunarCalendarFestivalUtils;
import net.mamoe.mirai.contact.Contact;
import net.mamoe.mirai.contact.Group;
import net.mamoe.mirai.message.data.At;
import net.mamoe.mirai.message.data.PlainText;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author: 邢晨旭
 * @Date: 2021/6/22 09:33
 * @Description:
 */
@Service
public class DailyFacade implements BaseFacade {
    public KeyWord keyWord = KeyWord.DAILY;

    @Resource
    LunarCalendarFestivalUtils utils;

    @Override
    public void execute(Contact sender, Contact receiver, String content) {
        String daily = HttpUtil.get(JX3API.DAILY);
        Result result = JSONObject.parseObject(daily, Result.class);
        ResultData data = result.getData();
        StringBuilder s = new StringBuilder();
        s.append("今天是").append(DateUtils.getCommonlyDate()).append("周").append(data.getWeek());
        s.append("\n农历").append(utils.getGanZhiYear()).append("【").append(utils.getAnimal()).append("】年 ")
                .append(utils.getLunarMonth()).append("月").append(utils.getLunarDay()).append("日");
        s.append(StringUtils.isEmpty(utils.getLunarTerm()) ? "" : "\n节气:" + utils.getLunarTerm());
        s.append(StringUtils.isEmpty(utils.getSolarFestival()) ? "" : "\n公历节日:" + utils.getSolarFestival());
        s.append(StringUtils.isEmpty(utils.getLunarFestival()) ? "" : "\n农历节日:" + utils.getLunarFestival());
        s.append("\n").append(data.getDaily());
        if (sender instanceof Group) {
            SenderHelper.sendMessage(receiver, new At(sender.getId()).plus(s));
        } else {
            //TODO 暂时没开放私聊
            SenderHelper.sendMessage(receiver, new PlainText(s));
        }
    }

    @Override
    public KeyWord keyWord() {
        return keyWord;
    }
}
